package com.base.cn.platform.os.common.rsa;

import java.net.URLDecoder;
import java.security.Key;
import java.util.Map;

/**
 * test
 *
 * @author s.li
 * @date 2019/03/15
 */
public class RSACoderTest {

    public static void main(String[] args) throws Exception {
        Map<String, Key> keyMap = RSAUtils.initKey();
        String publicKey = RSAUtils.getPublicKey(keyMap);
        String privateKey = RSAUtils.getPrivateKey(keyMap);

        System.err.println("公钥: \n\r" + publicKey);
        System.err.println("私钥： \n\r" + privateKey);

        System.err.println("公钥加密——私钥解密");
        String inputStr = "abc";
        byte[] encodedData = RSAUtils.encryptByPublicKey(inputStr, publicKey);
        byte[] decodedData = RSAUtils.decryptByPrivateKey(encodedData, privateKey);
        String outputStr = new String(decodedData);
        System.err.println("加密前: " + inputStr + "\n\r" + "解密后: " + outputStr);


        System.err.println("私钥加密——公钥解密");
        inputStr = "sign";
        byte[] data = inputStr.getBytes();
        encodedData = RSAUtils.encryptByPrivateKey(data, privateKey);
        decodedData = RSAUtils.decryptByPublicKey(encodedData, publicKey);
        outputStr = new String(decodedData);
        System.err.println("加密前: " + inputStr + "\n\r" + "解密后: " + outputStr);

        System.err.println("私钥签名——公钥验证签名");
        // 产生签名
        String sign = RSAUtils.sign(encodedData, privateKey);
        System.err.println("签名:" + sign);
        // 验证签名
        boolean status = RSAUtils.verify(encodedData, publicKey, sign);
        System.err.println("状态:" + status);


        byte[] encryptData = RSAUtils.encryptByPublicKey("123456",publicKey);
        System.out.println(RSAUtils.encryptBASE64(encryptData));;
        //======================================


        String str = new String(RSAUtils.decryptByPrivateKey("YudYFplwXWXs10SjKSHruy/DQLMyY2rqI61Q6bC7mHV6UAhBy8++m/oBilltkC2xs0/kRKkIOGn8HnPViv3KLJjYjDBG2iW29/DTaLsMa5FE0JNdqqP93MBowtaM+WHt8YYTv4cQeS0tnwWfHhX46BZ8WJtxVuJzsZVcG5bdyFg=","MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBALPqXQp8o/9/MSSAiCXug9+RK8NKrmLE24Kog4XzuRp3nPqItDcG+Fn5R9RFlakdCVq3NmW+iiz47rwbz57xAxKgWXm8pZczgDkmBktyr1LJjUgZtfaW1pFWFAMRoLkbLg1iaf5t7sqX0zgBQrUhf6Aycvn5bR71yT41VmLkgLR3AgMBAAECgYAOKEH8AkTRZQvT6H1OJs+ftwevjV/di8oOJP58euo05/alTD6KGfVO9yusy3D0ZsGGruYL6Ytr8IawrKuQwTxRh8qJztQgMCQp1d/DlKxZJujZhQBCqsVqIhr+9iGCsbqEs/U+20sNksWTtI+qBO7n6J9Og2qN0lT6qZcP1gJ3QQJBAPcBSP8jSbzd1pf+mLbFyTkKDEx95BOTcNEOpPv7e/JAFL6xSrBSveFyuPDompXDtVaJbcn+QiJnKdvQmrhj1NcCQQC6d6IaSrQbuUVQcfUh4QH7PcOgpvAPZTw+RpfC63C5rEZBPFwlkBe90M+TPNYPlcBKTcQ+eJSadFkGYxx1qolhAkEAmHf8nTElJSYyfqQzlC7GpSTkrprPHJ5Yd1N41+pGeiTAKW260thgSTfNnTeXpHaoCwDN+m+uVxojJoK/3q4RBQJBAKo1sBwN2vB/ozz2eQvu568eRyermvCCuYYjTD7aSbDDB/JelcKYEIPtSr2dR808JiXtbNiVmxiOYgFJkV37SKECQHVCNrr7CKx5+ALs0gB8Edl0671OpexHqy8a4//omBzkknuABMqhN8bWohltaYAB1IiL4dxsR1rqE8SSMHf/Ks0="));
        System.out.println("============================"+str);

        System.out.println(URLDecoder.decode("e%2Ff70YTlQ7dLiO8aJQ8lbB%2FIqN2zPmjS%2Fq9ajyHKSAb66eYc49aP98daoiydln9hEaVENNi2rictYc1uEVvX1H5DEsgPYGMmJsArnQB8g8HvMr%2BVdkXBdvVFDOKOxH41onTJw7ZdS2ruf%2BcVQjlPMY2nVhtzKmLRoW1KnBNEIX0%3D|96d6667e740100f00c4bfdab2bb7da1e","utf-8"));

    }
}
